﻿Imports DoNetDrive.Core
Imports DoNetDrive.Core.Command
Imports DoNetDrive.Core.Packet

Public Class JsonCommand
    Inherits Command.Text.TextCommand

    Protected mSendObject As Object
    Protected mCheckResult As Func(Of String, Boolean)
    ''' <summary>
    ''' 初始化两个重要参数，并进行参数检查
    ''' </summary>
    ''' <param name="cd">表示命令详情，包含通道信息，对端信息，超时时间，重发次数</param>
    Sub New(cd As INCommandDetail, par As Object, oCheckResult As Func(Of String, Boolean))
        MyBase.New(cd, Nothing)
        mSendObject = par
        _IsWaitResponse = True
        _Decompile = New JsonPacketDecompile()
        mCheckResult = oCheckResult
    End Sub

    ''' <summary>
    ''' 指令开始执行时，用于让命令组装第一个用于发送的数据包 CommandNext0 中组装（如果有的话）
    ''' </summary>
    Protected Overrides Sub CreatePacket()
        _Packet = New JsonPacket(mSendObject)
    End Sub

    ''' <summary>
    ''' 释放使用的资源
    ''' </summary>
    Protected Overrides Sub Release0()
        Return
    End Sub

    ''' <summary>
    ''' 检查并进行命令的下一部分
    ''' </summary>
    ''' <param name="readPacket">收到的数据包</param>
    Protected Overrides Sub CommandNext(readPacket As INPacket)
        If TypeOf readPacket Is JsonPacket Then
            If mCheckResult Is Nothing Then
                _Result = New JsonCommandResult(readPacket)
                CommandCompleted()
            Else
                Try
                    Dim jPck As JsonPacket = readPacket
                    Dim sJson = jPck.GetJson()
                    If mCheckResult(sJson) Then
                        CreateCommandResult(sJson)
                        CommandCompleted()
                    End If
                Catch ex As Exception

                End Try
            End If

        End If
    End Sub

    Protected Overridable Sub CreateCommandResult(ByVal sJson As String)
        _Result = New JsonCommandResult(sJson)
    End Sub


    ''' <summary>
    ''' 重发时的设置参数
    ''' </summary>
    Protected Overrides Sub CommandReSend()
        Return
    End Sub

    ''' <summary>
    ''' 检查命令参数
    ''' </summary>
    ''' <param name="value"></param>
    ''' <returns></returns>
    Protected Overrides Function CheckCommandParameter(value As INCommandParameter) As Boolean
        Return True
    End Function
End Class
